//
//  SEGAlertView.m
//  ExcellentHome
//
//  Created by 刘山 on 16/7/19.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGAlertView.h"
#define AlertMargin 15
#define AlertFontSize 16
#define ContentFontSize 13

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGText.h>)
#import <Commom/SEGText.h>
#else
#import "SEGText.h"
#endif

@implementation SEGAlertViewShowType
- (instancetype)initWithTitle:(NSString*)title ActionType:(UIAlertActionStyle)actionType
{
    if (self == [super init]) {
        _title = title;
        _actionStyle = actionType;
    }
    return self;
}

+ (instancetype)alertViewShowTypeWithTitle:(NSString*)title ActionType:(UIAlertActionStyle)actionType
{
    return [[SEGAlertViewShowType alloc] initWithTitle:title ActionType:actionType];
}
@end

@implementation SEGAlertView

+ (void)showAlertWithDlegate:(id)delegate withTitleArray:(NSArray*)titleArray destructiveButtonIndex:(NSInteger)index
{
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:delegate cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
    for (NSString* title in titleArray) {
        [sheet addButtonWithTitle:title];
    }
    
    if (index < titleArray.count) {
        [sheet setDestructiveButtonIndex:index];
    }
    
    [sheet showInView:[UIApplication sharedApplication].keyWindow];
}

+ (void)showAlertWithTitle:(NSString*)title Message:(NSString*)msg ShowActionStyle:(NSArray<SEGAlertViewShowType*>*)showType Hander:(SEGAlertViewHander)confirm
{
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction* _Nonnull action) {
        confirm(action);
    }];
    
    [alertController addAction:cancelAction];
    
    for (SEGAlertViewShowType* str in showType) {
        UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:str.title style:str.actionStyle handler:^(UIAlertAction* _Nonnull action) {
            confirm(action);
        }];
        [alertController addAction:confirmAction];
    }
    
    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)showTmepMessageWithMessage:(NSString*)msg withtime:(CGFloat)time
{
    if (msg && [msg isKindOfClass:[NSString class]] && [msg length] > 0) {
        
        CGSize contextSize = [SEGUtils sizeWithConstrainedWithString:msg ToWidth:LYScreenWidth() - 80 WithFontOfSize:AlertFontSize];
        
        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((LYScreenWidth() - contextSize.width) * 0.5 - AlertMargin, (LYScreenHeight() - contextSize.height) * 0.5, contextSize.width + AlertMargin * 2, contextSize.height + AlertMargin * 2)];
        
        [titleLabel setText:msg];
        [titleLabel setFont:[UIFont systemFontOfSize:AlertFontSize]];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleLabel setTextColor:[UIColor whiteColor]];
        titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        titleLabel.layer.cornerRadius = 5.0f;
        [titleLabel.layer setMasksToBounds:YES];
        titleLabel.numberOfLines = 0;
        [[UIApplication sharedApplication].keyWindow addSubview:titleLabel];
        titleLabel.alpha = 0;
        
        [UIView animateWithDuration:0.2 animations:^{
            titleLabel.alpha = 1;
        }
                         completion:^(BOOL finished) {
                             
                             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                 [UIView animateWithDuration:0.2 animations:^{
                                     titleLabel.alpha = 0;
                                 }
                                                  completion:^(BOOL finished) {
                                                      [titleLabel removeFromSuperview];
                                                  }];
                             });
                             
                         }];
    }
}

+ (void)showTmepMessageWithMessage:(NSString*)msg
{
    [SEGAlertView showTmepMessageWithMessage:msg withtime:0.4];
}

+ (void)showTmepMessageWithMessage:(NSString*)msg preferredStyle:(UIAlertControllerStyle)preferredStyle
{
    [self showTmepMessageWithMessage:msg];
}

+ (void)showTmepMessageWithTitle:(NSString*)title message:(NSString*)msg{
    [self showTmepMessageWithMessage:msg withtime:0.4 WithTitle:title];
}

+ (void)showTmepMessageWithMessage:(NSString*)msg withtime:(CGFloat)time WithTitle:(NSString*)title
{
    if (msg && [msg isKindOfClass:[NSString class]] && [msg length] > 0) {
        
        CGSize titleSize = [SEGUtils sizeWithConstrainedWithString:title ToWidth:LYScreenWidth() - 60 WithFontOfSize:AlertFontSize];
        CGSize contentSize = [SEGUtils sizeWithConstrainedWithString:msg ToWidth:LYScreenWidth() - 60 WithFontOfSize:ContentFontSize];
        
        //背景色
        CGFloat w =  titleSize.width > contentSize.width ? titleSize.width : contentSize.width;
        UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(
                                                                         (LYScreenWidth() - w - 30) * 0.5,
                                                                         (LYScreenHeight() - titleSize.height - contentSize.height - 40) * 0.5,
                                                                         w + 30,
                                                                         titleSize.height + contentSize.height + 40
                                                                         )];
        [backGroundView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
        backGroundView.layer.cornerRadius = 5.0f;
        [backGroundView.layer setMasksToBounds:YES];
        backGroundView.alpha = 1;
        
        
        
        //标题
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(
                                                                       CGRectGetMidX(backGroundView.frame) -  titleSize.width * 0.5,
                                                                       CGRectGetMinY(backGroundView.frame) + 15,
                                                                       titleSize.width ,
                                                                       titleSize.height)];
        [titleLabel setFont:[UIFont systemFontOfSize:AlertFontSize]];
        titleLabel.textColor = kColor_T;
        titleLabel.text = title;
        titleLabel.alpha = 0;
        
        //内容
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(
                                                                         CGRectGetMidX(backGroundView.frame) -  contentSize.width * 0.5,
                                                                         CGRectGetMaxY(titleLabel.frame) + 10,
                                                                         contentSize.width ,
                                                                         contentSize.height)];
        [contentLabel setFont:[UIFont systemFontOfSize:ContentFontSize]];
        contentLabel.textColor = [UIColor whiteColor];
        contentLabel.numberOfLines = 0;
        contentLabel.text = msg;
        titleLabel.alpha = 0;
        
        [[UIApplication sharedApplication].keyWindow addSubview:backGroundView];
        [[UIApplication sharedApplication].keyWindow addSubview:titleLabel];
        [[UIApplication sharedApplication].keyWindow addSubview:contentLabel];
        
        [UIView animateWithDuration:0.2 animations:^{
            backGroundView.alpha = 1;
            titleLabel.alpha = 1;
            contentLabel.alpha = 1;
        }
                         completion:^(BOOL finished) {
                             
                             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                 [UIView animateWithDuration:0.2 animations:^{
                                     backGroundView.alpha = 0;
                                     titleLabel.alpha = 0;
                                     contentLabel.alpha = 0;
                                 }
                                                  completion:^(BOOL finished) {
                                                      [backGroundView removeFromSuperview];
                                                      [titleLabel removeFromSuperview];
                                                      [contentLabel removeFromSuperview];
                                                  }];
                             });
                             
                         }];
    }
}

/// 带有图片的提示消息
/// @param msg msg
/// @param image image
+ (void)showTmepMessageWithMessage:(NSString *)msg image:(UIImage *)image {
     [SEGAlertView showTmepMessageWithMessage:msg withtime:0.4 image:image];
}

/// 带有图片的提示消息
/// @param msg msg
/// @param time time
/// @param image image
+ (void)showTmepMessageWithMessage:(NSString*)msg withtime:(CGFloat)time image:(UIImage *)image {
    if (msg && [msg isKindOfClass:[NSString class]] && [msg length] > 0) {
        
        CGSize msgSize = [SEGUtils sizeWithConstrainedWithString:msg ToWidth:LYScreenWidth() - 80 WithFontOfSize:AlertFontSize];
        if (msgSize.width < 120) msgSize.width = 120;
        if (msgSize.width > kScreenWidth-80) msgSize.width = kScreenWidth-80;
        
        // 背景
        UIView *bgView = [[UIView alloc] init];
        bgView.bounds = CGRectMake(0, 0, msgSize.width + AlertMargin*2,  msgSize.height + AlertMargin*2);
        bgView.center = CGPointMake(LYScreenWidth()/2, LYScreenHeight()/2);
        bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        bgView.alpha = 0;
        bgView.layer.cornerRadius = 3.0f;
        [bgView.layer setMasksToBounds:YES];
        [[UIApplication sharedApplication].keyWindow addSubview:bgView];
 
        // 提示文字
         UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AlertMargin, AlertMargin, bgView.width-AlertMargin*2, msgSize.height)];
         [titleLabel setText:msg];
         [titleLabel setFont:[UIFont systemFontOfSize:AlertFontSize]];
         [titleLabel setTextAlignment:NSTextAlignmentCenter];
         [titleLabel setTextColor:[UIColor whiteColor]];
         titleLabel.numberOfLines = 0;
         [bgView addSubview:titleLabel];
        // 图片
        if (image) {
            bgView.bounds = CGRectMake(0, 0, msgSize.width + AlertMargin*2, 10+ image.size.height + 10 + msgSize.height + 15);

            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(0, 10, image.size.width, image.size.height);
            imageView.centerX = bgView.width/2;
            imageView.image = image;
            [bgView addSubview:imageView];
            
            titleLabel.frame = CGRectMake(AlertMargin, imageView.bottom +10, bgView.width-AlertMargin*2, msgSize.height);
        }
        
        [UIView animateWithDuration:0.2 animations:^{
            bgView.alpha = 1;
        } completion:^(BOOL finished) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.2 animations:^{
                    bgView.alpha = 0;
                } completion:^(BOOL finished) {
                    [bgView removeFromSuperview];
                }];
            });
        }];
    }
}

#pragma mark - 穿越提示
+ (void)showRefrshTipsWithMessage:(NSString *)msg parentView:(UIView *)parentView
{
    //    CGFloat padding = 10;
    //先移除之前的试图，防止重叠
    [self removeRefrshTipsWithParentView:parentView];
    
    SEGLabel *label = [SEGLabel new];
    label.tag = 555;
    label.text = msg;
    label.font = [UIFont systemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = kColor_W;
    label.backgroundColor = kColor_T;
    label.width = parentView.width;
    label.height = 40;
    label.bottom =  0;
    label.alpha = 1;
    [parentView addSubview:label];
    [UIView animateWithDuration:0.3 animations:^{
        label.top = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 delay:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            label.bottom = 0;
            label.alpha = 0.4;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }];
}

+ (void)removeRefrshTipsWithParentView:(UIView *)parentView
{
    [parentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == 555) {
            [obj removeFromSuperview];
            *stop = YES;
        }
    }];
    
}

@end
